﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.CoreBusinessLogic.Projects;
using SCMS.CoreBusinessLogic.Web;
using SCMS.Model;
using SCMS.CoreBusinessLogic.Security;
using SCMS.CoreBusinessLogic.ActionFilters;

namespace SCMS.UI.Controllers
{
    [MyException]
    public class ProjectController : PortalBaseController
    {
        private IProjectService projectService;
        private List<Donor> _donors;
        private List<ProjectDonor> _projectDonors;
        private List<Currency> _currencies;

        public ProjectController(IPermissionService permissionService, IUserContext userContext, IProjectService projectService)
            : base(userContext, permissionService)
        {
            this.projectService = projectService;
        }

        //
        // GET: /Project/

        public ActionResult Index()
        {

            _projectDonors = new List<ProjectDonor>();
            _projectDonors = projectService.GetCurrentProjectDonors(countryProg);
            return View(_projectDonors);
        }

        //
        // GET: /Project/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {

            Models.Project project = new Models.Project();
            project.EntityProject = new Project();
            _donors = projectService.GetDonors(countryProg.Id);
            _currencies = projectService.GetCurrencies(countryProg.Id);
            project.Donors = new SelectList(_donors, "Id", "Name");
            project.Currencies = new SelectList(_currencies, "Id", "Name");
            project.StartDate = project.EndDate = DateTime.Now;
            ViewBag.FormHeader = "New Project";
            ViewBag.ButtonText = "Add Project";
            return View(project);

        }

        [HttpPost]
        public ActionResult SaveProject(Models.Project project)
        {

            string pdId = "";
            if (project.Id == null || project.Id.Trim() == "")
            {
                projectService.CreateProject(project.EntityProject);
                projectService.CreateProjectDonor(project.EntityProject, project.StartDate, project.EndDate, project.ProjectManagerId, project.DonorId, project.CurrencyId, ref pdId);
            }
            else
            {
                projectService.UpdateProject(project.EntityProject);
                projectService.UpdateProjectDonor(project.Id, project.EntityProject.ProjectNumber, project.StartDate, project.EndDate, project.ProjectManagerId, project.DonorId, project.CurrencyId, ref pdId);
            }
            _projectDonors = projectService.GetCurrentProjectDonors(countryProg);
            return View("ProjectList", _projectDonors);

        }

        public ActionResult ProjectList()
        {

            _projectDonors = projectService.GetCurrentProjectDonors(countryProg);
            return View("ProjectList", _projectDonors);

        }

        //
        // POST: /Project/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {

            // TODO: Add insert logic here
            return RedirectToAction("Index");

        }

        //
        // GET: /Project/Edit/5

        public ActionResult Edit(string id)
        {

            ProjectDonor pd = projectService.GetProjectDonorById(id);
            Models.Project project = new Models.Project();
            project.Id = pd.Id.ToString();
            project.EntityProject = pd.Project;
            project.CurrencyId = pd.CurrencyId.ToString();
            project.DonorId = pd.DonorId.ToString();
            project.StartDate = (DateTime)pd.StartDate;
            project.EndDate = (DateTime)pd.EndDate;
            _donors = projectService.GetDonors(countryProg.Id);
            _currencies = projectService.GetCurrencies(countryProg.Id);
            project.Donors = new SelectList(_donors, "Id", "Name");
            project.Currencies = new SelectList(_currencies, "Id", "Name");
            ViewBag.FormHeader = "Project: " + pd.ProjectNumber;
            ViewBag.ButtonText = "Save Project";
            return View("Create", project);

        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {

            // TODO: Add update logic here

            return RedirectToAction("Index");

        }

        //
        // GET: /Project/Delete/5

        public ActionResult Delete(string id)
        {

            projectService.DeleteProjectDonor(id);
            return ProjectList();

        }

        //
        // POST: /Project/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {

            // TODO: Add delete logic here

            return RedirectToAction("Index");

        }
    }
}
